﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EzyGoEzySystem.Models;

namespace EzyGoEzySystem.Services
{
    public static class DataAccess
    {
        public static bool IsUsernameExisted(string username)
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                return (from account in dbContext.Accounts
                        where account.Username == username
                        select account).Any();
            }
        }

        public static void AddNewAccount(RegisterModel account)
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                var newAccount = new Account();
                
                newAccount.Balance = 0;
                newAccount.Email = account.Email;
                newAccount.MacAddress = account.MacAddress;
                newAccount.Name = account.Name;
                newAccount.Password = account.Password;
                newAccount.PaymentMethod = account.PaymentMethod;
                newAccount.PhoneNumber = account.PhoneNumber;
                newAccount.Username = account.Username;
                newAccount.CreditCard = account.CreditCard;
    
                dbContext.Accounts.Add(newAccount);
                dbContext.SaveChanges();
            }
        }

        public static bool IsUserExisted(string username, string password)
        {
            password = Encryptor.Encrypt(password);
            using (var dbContext = new EzyGoEzyDBContext())
            {
                return (from account in dbContext.Accounts
                        where account.Username == username && account.Password == password
                        select account).Any();
            }
        }

        public static Account GetAccount(string username)
        {
            using  (var  dbContext = new  EzyGoEzyDBContext())
            {
                var  entries = from  entry in  dbContext.Accounts
                              where  entry.Username == username
                              select  entry;
                return  entries.First();
            }
        }

        public static PaymentJournalEntry GetPaymentJournalEntry(string invoice)
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                var entries = from entry in dbContext.PaymentJournalEntries
                              where entry.Invoice == invoice
                              select entry;
                return entries.First();
            }
        }

        public static PaymentJournalEntry GetPaymentJournalEntry(string invoice, EzyGoEzyDBContext dbContext)
        {
            var entries = from entry in dbContext.PaymentJournalEntries
                          where entry.Invoice == invoice
                          select entry;
            return entries.First();
        }
        public static List<BusStop> GetBusStop()
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                return dbContext.BusStop.ToList<BusStop>();
                
            }
        }

        public static IEnumerable<TravelJournalEntry> GetTravelJournalEntries()
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                return dbContext.TravelJournalEntries.ToList<TravelJournalEntry>();
            }
        }

        public static List<string> GetPaymentMethods()
        {
            var paymentMethodList = new List<string>();
            paymentMethodList.Add("Pre-pay");
            paymentMethodList.Add("Post-pay");
            return paymentMethodList;
        }

        public static Account UpdateAccountDetails(int accountID, ProfileModel profileModel)
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                var account = dbContext.Accounts.Find(accountID);
                account.Email = profileModel.Email;
                account.MacAddress = profileModel.MacAddress;
                account.Name = profileModel.Name;
                account.PhoneNumber = profileModel.PhoneNumber;
                account.PaymentMethod = profileModel.PaymentMethod;
                account.CreditCard = profileModel.CreditCard;

                dbContext.SaveChanges();
                return account;
            }
        }

        public static ProfileModel GetCorrespondingAccount(Account account)
        {
            if (account == null)
            {
                return new ProfileModel();
            }

            var profileModel = new ProfileModel
            {
                Email = account.Email,
                MacAddress = account.MacAddress,
                Name = account.Name,
                PaymentMethod = account.PaymentMethod,
                PhoneNumber = account.PhoneNumber,
                PaymentMethods = new System.Web.Mvc.SelectList(GetPaymentMethods()),
                CreditCard = account.CreditCard
            };

            return profileModel;
        }
    }
}